package com.android.gpuimage;

import android.graphics.Matrix;
import android.graphics.RectF;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by wu-mingguang on 2018/8/3.
 */

/* compiled from: ZoomImageView */
public class DataQueueUtil {
    private static MatrixQueue matrixQueue = new MatrixQueue(16);
    private static RectFQueue rectFQueue = new RectFQueue(16);

    public static Matrix obtainMatrix() {
        return (Matrix) matrixQueue.poll();
    }

    public static Matrix obtainMatrix(Matrix matrix) {
        Matrix obtained = (Matrix) matrixQueue.poll();
        if (matrix != null) {
            obtained.set(matrix);
        }
        return obtained;
    }

    public static void offerMatrix(Matrix matrix) {
        matrixQueue.offer(matrix);
    }

    public static RectF obtainRectF() {
        return (RectF) rectFQueue.poll();
    }

    public static RectF obtainRectF(float f, float f2, float f3, float f4) {
        RectF rectF = (RectF) rectFQueue.poll();
        rectF.set(f, f2, f3, f4);
        return rectF;
    }

    public static void offerRectF(RectF rectF) {
        rectFQueue.offer(rectF);
    }

    /**
     * 获得RectF对角线长度
     */
    public static float getRectFDiagonalLength(float left, float top, float right, float bottom) {
        float x = left - right;
        float y = top - bottom;
        return (float) Math.sqrt((double) ((x * x) + (y * y)));
    }

    /**
     * 获取RectF中心
     */
    public static float[] getRectFCenter(float left, float top, float right, float bottom) {
        return new float[]{(left + right) / 2.0f, (top + bottom) / 2.0f};
    }

    /**
     * 获得一个Matrix的第0和第4个元素
     * [0] 1 2
     * 3 [4] 5
     * 6 7 8
     */
    public static float[] getDiagonalValues(Matrix matrix) {
        if (matrix == null) {
            return new float[2];
        }
        float[] matrixElements = new float[9];
        matrix.getValues(matrixElements);
        return new float[]{matrixElements[0], matrixElements[4]};
    }

    public static float[] reversePoint(float[] source, Matrix matrix) {
        if (source == null || matrix == null) {
            return new float[2];
        }
        float[] target = new float[2];
        Matrix obtained = obtainMatrix();
        matrix.invert(obtained);
        obtained.mapPoints(target, source);
        offerMatrix(obtained);
        return target;
    }

    /* compiled from: ZoomImageView */
    private static abstract class MyQueue<T> {
        private int capacity;
        private Queue<T> queue = new LinkedList();

        protected abstract T reset(T t);

        protected abstract T obtain();

        public MyQueue(int capacity) {
            this.capacity = capacity;
        }

        public T poll() {
            if (queue.size() == 0) {
                return obtain();
            }
            return reset(queue.poll());
        }

        public void offer(T t) {
            if (t != null && queue.size() < capacity) {
                queue.offer(t);
            }
        }
    }

    /* compiled from: ZoomImageView */
    private static class MatrixQueue extends MyQueue<Matrix> {
        public MatrixQueue(int capacity) {
            super(capacity);
        }

        protected Matrix newOne() {
            return new Matrix();
        }

        @Override
        protected Matrix obtain() {
            return newOne();
        }

        @Override
        protected Matrix reset(Matrix matrix) {
            matrix.reset();
            return matrix;
        }
    }

    /* compiled from: ZoomImageView */
    private static class RectFQueue extends MyQueue<RectF> {

        public RectFQueue(int capacity) {
            super(capacity);
        }

        protected RectF newOne() {
            return new RectF();
        }

        @Override
        protected RectF obtain() {
            return newOne();
        }

        @Override
        protected RectF reset(RectF rectF) {
            rectF.setEmpty();
            return rectF;
        }
    }
}
